package cn.wolfcode.car.business.web.controller.system;


import cn.wolfcode.car.business.domain.BpmnInfo;
import cn.wolfcode.car.business.query.BpmnInfoQuery;
import cn.wolfcode.car.business.service.IBpmnInfoService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.web.AjaxResult;
import org.apache.poi.util.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;

/**
 * 客户控制器
 */
@Controller
@RequestMapping("/business/bpmnInfo")
public class BpmnInfoController {
    //模板前缀
    private static final String prefix = "business/bpmnInfo/";

    @Autowired
    private IBpmnInfoService bpmnInfoService;
    @Autowired
    private IStatementService statementService;
    //页面------------------------------------------------------------
    //列表
    @RequestMapping("/listPage")
    public String listPage(){
        return prefix + "list";
    }

    @RequestMapping("/deployPage")
    public String deployPage(){
        return prefix + "deployPage";
    }

    @RequestMapping("/addPage")
    public String addPage(){
        return prefix + "add";
    }


    @RequestMapping("/editPage")
    public String editPage(Long id, Model model){
        model.addAttribute("bpmnInfo", bpmnInfoService.get(id));
        return prefix + "edit";
    }
    @RequestMapping("/uploadBpmnFile")
    @ResponseBody
    public AjaxResult uploadBpmnFile(MultipartFile file) throws IOException {
        String path = bpmnInfoService.uploadBpmnfile(file);

        return AjaxResult.success("操作成功",path);
    }
    @RequestMapping("/deploy")
    @ResponseBody
    public AjaxResult deploy(String path,String info,String bpmnType) throws IOException {

        bpmnInfoService.deploy( path, info, bpmnType);

        return AjaxResult.success();
    }
    //数据-----------------------------------------------------------
    //列表
    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<BpmnInfo> query(BpmnInfoQuery qo){
        System.out.println(bpmnInfoService.query(qo));
        return bpmnInfoService.query(qo);
    }


    //新增
    @RequestMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BpmnInfo bpmnInfo){

        bpmnInfoService.save(bpmnInfo);
        return AjaxResult.success();
    }

    //编辑
    @RequestMapping("/edit")
    @ResponseBody
    public AjaxResult edit(BpmnInfo bpmnInfo){
        bpmnInfoService.update(bpmnInfo);
        return AjaxResult.success();
    }

    //删除
    @RequestMapping("/remove")
    @ResponseBody
    public AjaxResult remove(Long id){
        bpmnInfoService.remove(id);
        return AjaxResult.success();
    }

    @RequestMapping("/readResource")
    public void readResource(String deploymentId, String type, HttpServletResponse response) throws IOException {
        InputStream inputStream = bpmnInfoService.readResource(deploymentId, type);
        // 最后调用IOUtils.copy api复制到前端显示
        IOUtils.copy(inputStream,response.getOutputStream());
    }



}
